//
//  TTAlertView.m
//  DataPickerView
//
//  Created by 王奎卿 on 2017/5/23.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "TTAlertView.h"
#import "AppDelegate.h"

//#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
//#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface TTAlertView () {
    CGFloat font;
    CGFloat center_width;
    CGFloat center_height;
    CGFloat default_height;
    CGFloat message_height;
}
/** 中间视图*/
@property (nonatomic, strong) UIView *centerView;
/** 标题*/
@property (nonatomic, strong) UILabel *titleLbl;
/** 信息*/
@property (nonatomic, strong) UILabel *messageLbl;
/** 分割线*/
@property (nonatomic, strong) UILabel *cutOffLine;
/** 取消*/
@property (nonatomic, strong) UIButton *cancelBtn;
/** 取消 | 确定 分割线*/
@property (nonatomic, strong) UILabel *cutCancelAndSure;
/** 确定*/
@property (nonatomic, strong) UIButton *sureBtn;

@property (nonatomic, copy) NSString *message;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *cancelBtnTitle;
@property (nonatomic, copy) NSString *sureBtnTitle;

@end

@implementation TTAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [app.window addSubview:self];
        center_width = SCREEN_WIDTH *0.85;
        if (SCREEN_WIDTH <= 321) {
            default_height = 35;
            font = 14;
        } else if (SCREEN_WIDTH >= 375 && SCREEN_WIDTH < 413) {
            default_height = 40;
            font = 15;
        } else if (SCREEN_WIDTH >= 414) {
            default_height = 45;
            font = 16;
        }
    }
    return self;
}

+ (instancetype)alertViewWithTitle:(NSString *)title Message:(NSString *)message cancelBtnTitle:(NSString *)cancel sureBtnTitle:(NSString *)sure sureBtnClick:(alertSure)sureBlock cancelBtnClick:(alertCancel)cancelBlock {
    TTAlertView *alertView = [[TTAlertView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    alertView.backgroundColor = RGBA(1, 1, 1, 0.5);
    alertView.cancelBtnTitle  = cancel;
    alertView.sureBtnTitle    = sure;
    alertView.message         = message;
    alertView.sure_block      = sureBlock;
    alertView.cancel_block    = cancelBlock;
    alertView.title           = title;
    return alertView;
}
- (void)setUpViews {
    
}


- (void)layoutViews {

    
}


//- (void)getCenterViewHeight {
//    // 创建label
//    _messageLbl = [[UILabel alloc] init];
//    _messageLbl.backgroundColor = [UIColor whiteColor];
//    _messageLbl.numberOfLines = 0;
//    _messageLbl.textColor = RGBA(100, 100, 100, 1);
//    
//    // 设置label的属性
//    _messageLbl.font = [UIFont systemFontOfSize:17];
//    _messageLbl.text = _message;
//    
//    CGFloat labelWidth = center_width -20;
//    // 设置文字属性 要和label的一致
//    NSDictionary *attrs = @{NSFontAttributeName : _messageLbl.font};
//    CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
//    
//    // 计算文字占据的高度
//    CGSize size = [_message boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
//    
//    // 设置label尺寸
//    _messageLbl.frame = CGRectMake(10, default_height, size.width, size.height +20);
//    message_height = size.height +20;
//    center_height = default_height +message_height +default_height +1;
//}

#pragma mark - Action
- (void)sureBtnClick {
    [self removeFromSuperview];
    self.sure_block();
}

- (void)cancelBtnClick {
    [self removeFromSuperview];
    self.cancel_block();
}

#pragma mark - Set
- (void)setMessage:(NSString *)message {
//    [self getCenterViewHeight];
    [self addSubview:self.centerView];
    [self.centerView addSubview:self.titleLbl];
    [self.centerView addSubview:self.messageLbl];
    [self.centerView addSubview:self.cutOffLine];
    if (_cancelBtnTitle) { // 如果取消按钮有值 则将取消按钮和分割线加到centerView上
        [self.centerView addSubview:self.cancelBtn];
        [self.centerView addSubview:self.cutCancelAndSure];
    }
    [self.centerView addSubview:self.sureBtn];
    _messageLbl.text = message;
}

- (void)setCancelBtnTitle:(NSString *)cancelBtnTitle {
    _cancelBtnTitle = cancelBtnTitle;
}

- (void)setSureBtnTitle:(NSString *)sureBtnTitle {
    _sureBtnTitle = sureBtnTitle;
}

- (void)setTitle:(NSString *)title {
    _titleLbl.text = title;
}

#pragma mark - 懒加载
/** 中间视图*/
- (UIView *)centerView {
    if (!_centerView) {
       _centerView = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - center_width) / 2, SCREEN_HEIGHT / 2 - (default_height *3 +10)/2, center_width, default_height *3 +10)];
        _centerView.backgroundColor = [UIColor whiteColor];
        _centerView.layer.cornerRadius = 5;
        _centerView.layer.masksToBounds = YES;
    }
    return _centerView;
}

- (UILabel *)titleLbl {
    if (!_titleLbl) {
        _titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, center_width, default_height +10)];
        _titleLbl.textAlignment = NSTextAlignmentCenter;
        _titleLbl.font = [UIFont systemFontOfSize:font +2];
    }
    return _titleLbl;
}

- (UILabel *)messageLbl {
    if (!_messageLbl) {
        _messageLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, default_height, center_width, default_height)];
        _messageLbl.textAlignment = NSTextAlignmentCenter;
        _messageLbl.font = [UIFont systemFontOfSize:font];
    }
    return _messageLbl;
}

- (UILabel *)cutOffLine {
    if (!_cutOffLine) {
        _cutOffLine = [[UILabel alloc] initWithFrame:CGRectMake(0, default_height *2 , center_width, 1)];
        _cutOffLine.backgroundColor = RGBA(177, 184, 186, 1);
    }
    return _cutOffLine;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, default_height *2 +1, center_width /2 -0.5, default_height)];
        [_cancelBtn setTitle:_cancelBtnTitle forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:RGBA(60, 60, 60, 1) forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:font];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UILabel *)cutCancelAndSure {
    if (!_cutCancelAndSure) {
        _cutCancelAndSure = [[UILabel alloc] initWithFrame:CGRectMake(center_width /2 -0.5, default_height +message_height +1, 1, default_height)];
        _cutCancelAndSure.backgroundColor = RGBA(235, 52, 68, 1);
    }
    return _cutCancelAndSure;
}

- (UIButton *)sureBtn {
    if (!_sureBtn) {
        if (_cancelBtnTitle) {
            _sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(center_width/2 +0.5, default_height +message_height +6, center_width /2 -0.5, default_height)];
            [_sureBtn setTitle:_sureBtnTitle forState:UIControlStateNormal];
            [_sureBtn setTitleColor:RGBA(60, 60, 60, 1) forState:UIControlStateNormal];
            _sureBtn.titleLabel.font = [UIFont systemFontOfSize:font];
            [_sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
        } else {
            _sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, default_height *2 +6, center_width, default_height)];
            [_sureBtn setTitle:_sureBtnTitle forState:UIControlStateNormal];
            [_sureBtn setTitleColor:RGBA(60, 60, 60, 1) forState:UIControlStateNormal];
            _sureBtn.titleLabel.font = [UIFont systemFontOfSize:font];
            [_sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return _sureBtn;
}

@end
